package br.com.rookgraph;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;


public class GrafoExemple {
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		BufferedReader input = prepareInput(args[0]);
		
		try {
			
			int size = Integer.parseInt(input.readLine());
			Graph grafo = new Graph(size);
			
			String aresta;
			String[] vertices;
			do{
				aresta = input.readLine();
				if(aresta != null){
					vertices = aresta.split(" ");
					if (vertices != null) {
						grafo.addEdge(Integer.valueOf(vertices[0]), Integer.valueOf(vertices[1]), 1);
					}
				}
			}while(aresta != null);
			
			
			input = prepareInput(args[1]);
			
			int sizeTree = Integer.parseInt(input.readLine());
			Graph tree = new Graph(size);
			
			do{
				aresta = input.readLine();
				if(aresta != null){
					vertices = aresta.split(" ");
					if (vertices != null) {
						tree.addEdge(Integer.valueOf(vertices[0]), Integer.valueOf(vertices[1]), 1);
					}
				}
			}while(aresta != null);
			
			
			System.out.println("           Grafo Original: ");
			grafo.showMatix();

			System.out.println("\n\n       Árvore de partida: \n");
			tree.showMatix();
			
					
			BranchReduceIR branchReduce = new BranchReduceIR(grafo, tree);
			Graph reduce = branchReduce.execute();
			System.out.println("\n\n          Aŕvore Final: \n");
			reduce.showMatix();
			
			
			
			
//			grafo.showEdges();
//			System.out.println("\n");
//			grafo.showMatix();
//
//
//			/* mostra as diversas arvores diferentes que podem ser geradas
//			 * apatir do grafo criado.                                     */			
//			Graph grafoTreeCurrent;
//			Graph grafoMaiorNumBranchs = grafo.getTree(new Graph(grafo.getOrder()), 1);
//			System.out.println("\n ___________________________ \n");
//			grafoMaiorNumBranchs.showMatix();
//			
//			for (int i = 2; i <= grafo.getOrder(); i++) {
//				grafoTreeCurrent = grafo.getTree(new Graph(grafo.getOrder()), i);
//				
//				System.out.println("\n> "+ (i));
//				grafoTreeCurrent.showMatix();
//				
//				int lengthCurrent = grafoTreeCurrent.getBranchs().length;
//				int lengthMaior = grafoMaiorNumBranchs.getBranchs().length;
//				
//				if(lengthCurrent > lengthMaior){
//					grafoMaiorNumBranchs = grafoTreeCurrent;
//				}
//				
//			}
//			
//			System.out.println("\n\n***************************\n");
//			
//			grafoMaiorNumBranchs.showMatix();
//			
//			BranchReduceIR branchReduce = new BranchReduceIR(grafo, grafoMaiorNumBranchs);
//			Graph reduce = branchReduce.execute();
//			
//			System.out.println("\n\n------ Reduce: -----\n");
//			reduce.showMatix();
			
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		
	}

	/**
	 * Prepara a leitura  com base no args do programa.
	 * O primeiro args especifica o arquivo de entrada, 
	 * se ele estiver null, adimite a entrada padrão. 
     *
	 * @param args
	 * @return
	 */
	private static BufferedReader prepareInput(String args) {
		try {
			
			Reader inputStreamReader = args == null ? new InputStreamReader(System.in) : new FileReader(new File(args));
			
			BufferedReader input = new BufferedReader(inputStreamReader);
			
			return input;
			
		} catch (FileNotFoundException e1) {
			e1.printStackTrace();
		}
		
		return null;
	}
	
}
